//Unit test for Salvager

#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <assert.h>

void randCards(struct gameState* state);

void fail(int n) {
    printf("FAIL !!!\n");
    exit(n);
}

int main(){
	struct gameState state;
	int player = 0;
	int i,j,k, prevHandCount, prevCoinCount, prevDiscardCount, trashCard, trashCardCost;
	srand (time(NULL));
	
	randCards(&state);
	i = rand() % state.handCount[player];
	state.hand[player][i] = salvager;
	
	state.playedCardCount = 0;
	state.numBuys = 1;
	state.coins = 0;
	prevCoinCount = state.coins;
	prevHandCount = state.handCount[player];
	prevDiscardCount = state.discardCount[player];
	
	do {
		trashCard = rand() % state.handCount[player];
	} while (i != trashCard);
	
	trashCardCost = getCost(state.hand[player][trashCard]);
	
	j = cardEffect(salvager, trashCard, 0, 0, &state, i, NULL);
	if(j != 0) fail(0);
	if(state.playedCards[state.playedCardCount - 1] != salvager) fail(0);
	if(state.numBuys != 2) fail(0);
	if(state.coins != (prevCoinCount + trashCardCost)) fail(0);
	if(state.handCount[player] != (prevHandCount - 2)) fail(0);

	printf("Passed.\n");
	return 0;
}

void randCards(struct gameState* state) {
	int i, p;
	
	for(p = 0; p < 4; ++p) {
		state->handCount[p] = 5;
		for (i = 0; i < state->handCount[p]; ++i) {
			state->hand[p][i] = rand() % 27;
		}
		
		state->discardCount[p] = 10;
		for (i = 0; i < state->discardCount[p]; ++i) {
			state->discard[p][i] = rand() % 27;
		}
		
		state->deckCount[p] = 10;
		for (i = 0; i < state->deckCount[p]; ++i) {
			state->deck[p][i] = rand() % 27;
		}
	}
}
